{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Array arr has 1 dimension.\n",
      "Array arr is size (6,)\n",
      "[1,2,3,4,5,6]\n"
     ]
    }
   ],
   "source": [
    "# 1d arrays\n",
    "arr = [1,2,3,4,5]\n",
    "append!(arr, [6])       # append function. The added element has to be an array!\n",
    "\n",
    "println(\"Array arr has \", ndims(arr), \" dimension.\")     # number of dimensions\n",
    "println(\"Array arr is size \", size(arr))      # size of the array\n",
    "println(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,3)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3x4 Array{Int64,2}:\n",
       " 1  4  7  10\n",
       " 2  5  8  11\n",
       " 3  6  9  12"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [1 2 3; 4 5 6; 7 8 9]\n",
    "println(size(A))\n",
    "\n",
    "# I can't seem to find an argument allowing you to reshape the other way\n",
    "B = reshape([1:12], 3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.0 2.0 3.0 4.0]\n",
      "[1.0 2.0 3.0 4.0]\n"
     ]
    }
   ],
   "source": [
    "A = reshape([1.0,2.0,3.0,4.0], 1,4)\n",
    "\n",
    "println(A)\n",
    "println(A * eye(4))     # yields the same result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2x2 Array{Float64,2}:\n",
       " 1.0          0.0\n",
       " 8.88178e-16  1.0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = [1 2; 3 4]\n",
    "B * inv(B)      # B * inv(B) returns the identity matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3\n",
      " 4 5 6]\n",
      "[1 4\n",
      " 2 5\n",
      " 3 6]\n"
     ]
    }
   ],
   "source": [
    "A = [1 2 3; 4 5 6]\n",
    "println(A)     # A\n",
    "println(A')    # A transpose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12,-3]\n",
      "[12,-3]\n"
     ]
    }
   ],
   "source": [
    "# ************* Eigenvalues and Eigenvectors ********************* #\n",
    "A = [2 -4; -1 -1]\n",
    "x = [4; -1]\n",
    "eigVal = 3\n",
    "\n",
    "println(A * x)\n",
    "println(eigVal * x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3.0,-2.0]"
     ]
    }
   ],
   "source": [
    "# Now we know A has a real eigenvalue, so let's compute it with julia's built in function.\n",
    "\n",
    "w, v = eig(A)\n",
    "print(w)     # these are the eigenvalues"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4.0,-1.0]\n",
      "Our original eigenvector was [4, -1]\n"
     ]
    }
   ],
   "source": [
    "# ok, so the square matrix A has two eigenvalues, 3 and -2.\n",
    "# but what about the corresponding eigenvector?\n",
    "\n",
    "v      # this is the normalized eigenvector corresponding to w[0], or 3.\n",
    "\n",
    "# let's unnormalize it to see if we were right.\n",
    "\n",
    "length = sqrt(x[1]^2 + x[2]^2)  # the length of our original eigenvector x\n",
    "println(v[:, 1] * length)\n",
    "\n",
    "println(\"Our original eigenvector was [4, -1]\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Woohoo! It's important to remember that all multiples of this \n",
    "# eigenvector will be an eigenvector of A corresponding to lambda."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "det(A) = -2.0"
     ]
    }
   ],
   "source": [
    "# ************************ Determinants ************************ #\n",
    "A = [1 2; 3 4]\n",
    "print(\"det(A) = \", det(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "3x3 Array{Float64,2}:\n",
       " -0.181497   0.0759015   0.980458 \n",
       " -0.652719   0.736431   -0.177838 \n",
       " -0.735538  -0.672241   -0.0841178,\n",
       "\n",
       "[63.2736,49.1928,7.77941],\n",
       "8x3 Array{Float64,2}:\n",
       " -0.153834    0.0679486  -0.133773  \n",
       " -0.143769    0.111793   -0.00897498\n",
       " -0.180203    0.100975    0.0725716 \n",
       " -0.158513    0.158485    0.208183  \n",
       " -0.70659    -0.697668   -0.0667992 \n",
       " -0.289349    0.312578    0.197973  \n",
       " -0.554039    0.602472   -0.211356  \n",
       " -0.0900781  -0.0123776   0.919288  )"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ************************ Single Value Decomposition ************************ #\n",
    "A = [1 2 3 4 5 6 7 8;\n",
    "    9 10 11 12 4 23 45 2;\n",
    "    5 3 5 2 56 3 6 4]\n",
    "\n",
    "U, s, V = svd(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10-element Array{Float64,1}:\n",
       " -0.144989\n",
       "  0.268551\n",
       " -1.22986 \n",
       " -0.598548\n",
       " -1.18673 \n",
       "  0.123412\n",
       "  1.01455 \n",
       " -0.216124\n",
       " -0.799739\n",
       " -1.32548 "
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Here are a few more things you should probably know!\n",
    "A = randn(10)   # a 4-element array of Gaussian distributed numbers. rand() is uniformly random."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1x3 BitArray{2}:\n",
       " false  false  true"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Element-wise comparisons using '.'\n",
    "A = [3 3 3]\n",
    "B = [2 3 4]\n",
    "A .< B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# There are basic statistic operations like mean(), std(), var()\n",
    "# as well as math functions like exp(), sin(), etc.\n",
    "A = [1 2 3; 4 5 6; 7 8 9]\n",
    "A[:]     # flattens array by column\n",
    "A[1, :]  # first row\n",
    "A[:, 1]  # first column\n",
    "A[2, 3]  # second row, third column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3x3 Array{Int64,2}:\n",
       " 1  2  3\n",
       " 4  5  6\n",
       " 7  8  9"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.3.11",
   "language": "julia",
   "name": "julia-0.3"
  },
  "language_info": {
   "name": "julia",
   "version": "0.3.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
